package cn.tedu.controller;
/*
   数据入库:插入数据
 */
import cn.tedu.pojo.Student;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;

@RestController
@CrossOrigin
@RequestMapping("stu")  //http://localhost:8080/stu/sql
public class StuMysql {
    @RequestMapping("sql")
    public Student sql(Student s) throws Exception {

        //使用《MySQL依赖包》，没有添加jdbc依赖
        Class.forName("com.mysql.cj.jdbc.Driver");
        String url = "jdbc:mysql:///abc?characterEncoding=utf8";
        Connection conn = DriverManager.getConnection(url, "root", "root");
        String sql = "insert into student values(?,?,?,?) ";
        PreparedStatement ps = conn.prepareStatement(sql);//
        ps.setInt(1,s.getId());//s为对象
        ps.setString(2,s.getName());
        ps.setInt(3,s.getAge());
        ps.setString(4,s.getSex());

        ps.executeUpdate();
        System.out.println("插入数据，入库成功");
        System.out.println(s);//后端打印
        ps.close();//释放资源
        conn.close();

        return s;//返回对象Student

    }
}
